import optionslib as ol
import numpy as np
from datetime import datetime

class SkewData:
    """
    Properties:
       callPrice
       putPrice
       strikes
       callMoneyness
    """
    
    def __init__(self,csvFile):
       self.date = datetime.now()
       inputData = open (csvFile,"r")
       #retrieve spot and dte
       metaData  = inputData.readline().split(',');
       self.spot = float(metaData[0])
       self.dte = float(metaData[1])
       #read records into array [call price | strike | put price]
       arr = []
       for line in inputData.readlines():
       # add a new sublist and loop over comma separated elements
           arr.append([])
           for i in line.split(','):
       # convert to float and append to the last element of the list
               arr[-1].append(float(i))

       # extract call, put prices and strikes
       t = np.transpose(arr)
       self.callPrice = t[0]
       # poly fitter x axis in decreasing moneyness
       self.putPrice = t[2][::-1]
       self.strikes = t[1]
       # Moneyness
       self.callMoneyness = ol.strike2Moneyness(self.spot,self.strikes)
       self.putMoneyness = ol.strike2Moneyness(self.spot,self.strikes, ol.OptionType.Put)
       
    def getSkewFits(self):
        self.skews = []
        self.skews..append(ol.leastSquareFit(self.callMoneyness, self.callPrice))
        self.skews.append(ol.leastSquareFit(self.putMoneyness, self.putPrice))           
       
    def getStraddle(self):
        return skews[0](0.0) + skews[1](0.0)
    
path = 'c:/ayoub/fim/Data/SPY/march.csv'
sd = SkewData(path)


    
    #def straddle(self):
    #    return (self.callFit(0)+self.putFit(0)
   # path = 'c:/ayoub/FIM/Data/SPY/march.csv'
   # sd = SkewData(path)
                
